<template>
	<div class="app-container">
		<jk-operation-card ref="box1" class="margin-bottom-10">
			<div class="flex-between-center-wrap">
				<div class="flex-start-center-wrap">
					<!--<jk-report-button :permission-prefix="prefix" class="margin-5-0" :loading="exportLoading" @click="onReportBtn"></jk-report-button>-->
				</div>
				<div class="flex-end-center-wrap">
					<jk-select v-model="searchParams.workshopId" :option-list="workshopList" placeholder="区域" size="small" class="search-bar-item-size2 margin-5-0 item-gap"></jk-select>
					<el-date-picker
						v-model="searchParams.belongDate"
						type="daterange"
						align="right"
						unlink-panels
						size="small"
						:clearable="false"
						class="item-gap"
						value-format="yyyy-MM-dd"
						range-separator="-"
						start-placeholder="开始日期"
						end-placeholder="结束日期"
					></el-date-picker>
					<jk-select v-model="searchParams.groupId" :option-list="groupList" placeholder="班组" clearable size="small" class="search-bar-item-size1 margin-5-0 item-gap"></jk-select>
					<jk-search-button @click="onSearchBtn"></jk-search-button>
				</div>
			</div>
		</jk-operation-card>
		<jk-card>
			<jk-table border :span-method="mergeColMethod" :loading="tableLoading" :data="tableData" :height="tableHeight + 20">
				<vxe-column title="日期" align="left" field="belongDate" :width="110" />
				<vxe-column title="班制" align="left" field="shiftTypeName" :width="90" />
				<vxe-column title="班次" align="left" field="shiftName" :width="180">
					<template slot-scope="scope">
						<span>{{ `${scope.row.shiftName} (${scope.row.beginTime} ~ ${scope.row.endTime})` }}</span>
					</template>
				</vxe-column>
				<vxe-column title="班组" align="center" field="groupName" :width="160">
					<template slot-scope="scope">
						<el-button size="mini" icon="el-icon-date" type="primary" plain @click="onClickGroup(scope.row)">{{ scope.row.groupName }}</el-button>
					</template>
				</vxe-column>
				<vxe-column title="区域" align="left" field="workshopName" :width="100" />
				<vxe-column title="创建人" align="left" field="creatorName" :min-width="100" />
				<vxe-column title="创建时间" align="left" field="createTime" :width="160" />
				<vxe-column title="修改人" align="left" field="updaterName" :min-width="100" />
				<vxe-column title="修改时间" align="left" field="updateTime" :width="160" />
			</jk-table>
			<!-- 分页组件 -->
			<footer ref="box2" class="flex-end-center margin-top-10">
				<jk-page
					:page-index="searchParams.pageNo"
					:page-sizes="searchParams.pageSizes"
					:page-size="searchParams.pageSize"
					:total="totalCount"
					@on-change="onPageChange"
				/>
			</footer>
		</jk-card>
		<!--查看班组人员-->
		<set-group-emp-dialog
			:readonly="true"
			:check-data="checkData"
			:dialog-state="setGroupEmpDialogState"
			@on-visible-change="(e) => setGroupEmpDialogState = e"
		></set-group-emp-dialog>
	</div>
</template>

<script>
    import calHeight from '/src/mixins/cal-table-height';
    import { pageConfig, formatDate } from '/src/utils/jk/jk-tool';
    import {
        workforcemanagementScheduleShiftDelete,
        workforcemanagementScheduleShiftPage,
        workforcemanagementScheduleShiftExportExcel,
        workforcemanagementScheduleShiftSubmitAudit,
        workforcemanagementScheduleShiftClearSubmitAudit,
        workforcemanagementScheduleShiftUpdateAudit,
        workforcemanagementScheduleShiftAudit,
        workforcemanagementScheduleShiftClearAudit, workforcemanagementScheduleShiftClose, workforcemanagementScheduleShiftClearClose
    } from '/src/api/schedule/scheduleShift';
    import setGroupEmpDialog from '../scheduling/set-group-emp-dialog';

    export default {
        name: 'ScheduleShift',
        components: {
            setGroupEmpDialog
        },
        mixins: [calHeight],
        data() {
            return {
                prefix: 'personnel:group',
                apiItem: {
                    'page': workforcemanagementScheduleShiftPage,
                    'exportExcel': workforcemanagementScheduleShiftExportExcel,
                    'delete': workforcemanagementScheduleShiftDelete,
                    'submit': workforcemanagementScheduleShiftSubmitAudit,
                    'cancelSubmit': workforcemanagementScheduleShiftClearSubmitAudit,
                    'submitAudit': workforcemanagementScheduleShiftUpdateAudit,
                    'audit': workforcemanagementScheduleShiftAudit,
                    'cancelAudit': workforcemanagementScheduleShiftClearAudit,
                    'close': workforcemanagementScheduleShiftClose,
                    'cancelClose': workforcemanagementScheduleShiftClearClose
                },
                checkData: {},
                groupList: [],
                dataId: null,
                workshopList: [],
                tableLoading: true,
                exportLoading: false,
                tableData: [],
                totalCount: 0,
                searchParams: {
                    pageNo: pageConfig.pageNo,
                    pageSize: pageConfig.pageSize,
                    groupId: null,
                    belongDate: [],
                    workshopId: null
                },
                setGroupEmpDialogState: false
            };
        },
        mounted() {
            this.getDependentData();
        },
        methods: {
            onClickGroup(item) {
                this.checkData = {
                    scheduleId: item.scheduleId,
                    date: item.belongDate,
                    shiftItem: {
                        id: item.scheduleShiftId,
                        shiftName: item.shiftName
                    },
                    scheduleGroupId: item.scheduleGroupId,
                    scheduleGroupName: item.groupName
                };
                this.setGroupEmpDialogState = true;
            },
            onSearchBtn() {
                this.$set(this.searchParams, 'pageNo', 1);
                this.getListRequest();
            },
            onPageChange(e, size) {
                this.$set(this.searchParams, 'pageNo', e);
                this.$set(this.searchParams, 'pageSize', size);
                this.getListRequest();
            },
            mergeColMethod({ row, _rowIndex, column, visibleData }) {
                if (row.belongDate && column.property === 'belongDate') {
                    const prevRow = visibleData[_rowIndex - 1];
                    let nextRow = visibleData[_rowIndex + 1];
                    if (prevRow && prevRow.belongDate === row.belongDate) {
                        return { rowspan: 0, colspan: 0 };
                    } else {
                        let countRowspan = 1;
                        while (nextRow && nextRow.belongDate === row.belongDate) {
                            nextRow = visibleData[++countRowspan + _rowIndex];
                        }
                        if (countRowspan > 1) {
                            return { rowspan: countRowspan, colspan: 1 };
                        }
                    }
                } else {
                    if (row.shiftTypeName && column.property === 'shiftTypeName') {
                        const prevRow = visibleData[_rowIndex - 1];
                        let nextRow = visibleData[_rowIndex + 1];
                        if (prevRow && prevRow.belongDate === row.belongDate && prevRow.shiftTypeName === row.shiftTypeName) {
                            return { rowspan: 0, colspan: 0 };
                        } else {
                            let countRowspan = 1;
                            while (nextRow && nextRow.belongDate === row.belongDate && nextRow.shiftTypeName === row.shiftTypeName) {
                                nextRow = visibleData[++countRowspan + _rowIndex];
                            }
                            if (countRowspan > 1) {
                                return { rowspan: countRowspan, colspan: 1 };
                            }
                        }
                    }
                }
            },
            async getDependentData() {
                this.tableLoading = true;
                this.$set(this.searchParams, 'belongDate', [formatDate(Date.now() - 7 * 24 * 60 * 60 * 1000), formatDate(Date.now())]);
                this.workshopList = this.$store.state.user.userWorkshop.workshopList;
                this.$set(this.searchParams, 'workshopId', this.$store.state.user.userWorkshop.defaultWorkshopId);
                this.groupList = this.$store.state.user.userGroup.groupList;
                this.$set(this.searchParams, 'groupId', this.$store.state.user.userGroup.defaultGroupId);
                await this.getListRequest();
                this.tableLoading = false;
            },
            onDetailBtn(e) {
                this.dataId = e.id;
                this.detailDialogState = true;
            },
            /** 查询列表 */
            getListRequest() {
                this.tableLoading = true;
                return this.apiItem['page'](this.searchParams).then(res => {
                    this.tableLoading = false;
                    if (res.data.status === 200) {
                        this.tableData = res.data.res;
                        this.totalCount = res.data.count;
                    }
                });
            },
            /** 导出按钮操作 */
            onReportBtn() {
                const params = { ...this.searchParams };
                params.pageNo = null;
                params.pageSize = null;
                this.$confirm('是否确认导出所有数据项?', { type: 'warning' }).then(() => {
                    this.exportLoading = true;
                    return this.apiItem['exportExcel'](params);
                }).then(res => {
                    if (res.data.status === 200) {
                        this.$download.excel(res, `${this.$route.meta.name}.xls`);
                        this.exportLoading = false;
                    }
                }).catch(() => {});
            }
        }
    };
</script>
